<template>
<div>
  <el-table :data="records" style="width: 100%" border>
    <el-table-column type="index" align="center" label="序号" width="80"></el-table-column>

    <el-table-column prop="skuName" label="名称" width="width"></el-table-column>

      <el-table-column prop="skuDesc" label="描述" width="width"></el-table-column>

      <el-table-column label="默认图片" width="120">
        <template slot-scope="{row}">
          <img :src="row.skuDefaultImg" alt="" style="width: 100px;height: 100px">
        </template>
      </el-table-column>

      <el-table-column prop="weight" align="center" label="重量(KG)" width="100"></el-table-column>

      <el-table-column prop="price" align="center" label="价格(元)" width="80"></el-table-column>

      <el-table-column prop="date" label="操作" width="width">
        <template slot-scope="{row,$index}">
          <hint-button type="success" @click="cancelSale(row)" v-if="row.isSale===1" icon="el-icon-sort-down" size="mini" title="下架"></hint-button>
          <hint-button type="success" @click="onSale(row)" v-else icon="el-icon-sort-up" size="mini" title="上架"></hint-button>
          <hint-button type="primary" @click="$message({type:'info',message:'正在开发中'})" icon="el-icon-edit" size="mini" title="修改"></hint-button>
          <hint-button type="info" @click="detail(row)" icon="el-icon-info" size="mini" title="查看消息"></hint-button>
          <hint-button type="danger" @click="deleteSku(row)" icon="el-icon-delete" size="mini" title="删除"></hint-button>
        </template>
      </el-table-column>
  </el-table>
  <!--分页器-->
  <el-pagination
    style="text-align: center"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="page"
    :page-sizes="[5, 10, 15]"
    :page-size="limit"
    layout="prev, pager, next, jumper, ->,total,sizes"
    :total="total">
  </el-pagination>
  <!--抽屉-->
  <el-drawer
    title="查看详情"
    size="50%"
    :visible.sync="drawer"
    :with-header="false">
    <el-row>
      <el-col :span="5">名称</el-col>
      <el-col :span="16">{{skuInfo.skuName}}</el-col>
    </el-row>
    <el-row>
      <el-col :span="5">描述</el-col>
      <el-col :span="16">{{skuInfo.skuDesc}}</el-col>
    </el-row>
    <el-row>
      <el-col :span="5">价格</el-col>
      <el-col :span="16">{{skuInfo.price}} 元</el-col>
    </el-row>
    <el-row>
      <el-col :span="5">平台属性</el-col>
      <el-col :span="16"><el-tag style="margin-right: 10px" type="success" v-for="attr in skuInfo.skuAttrValueList" :key="attr.id">{{attr.valueName}}</el-tag></el-col>
    </el-row>
    <el-row>
      <el-col :span="5">商品图片</el-col>
      <el-col :span="16">
        <el-carousel height="500px">
          <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
            <img :src="item.imgUrl" alt="" style="height: 100%;width: 100%">
          </el-carousel-item>
        </el-carousel>
      </el-col>
    </el-row>

  </el-drawer>
</div>
</template>

<script>
export default {
  name: 'sku',
  data(){
    return{
      page:1,
      limit:10,
      total:0,
      records:[],//sku列表的数据
      drawer:false,
      skuInfo: { },
    }
  },
  mounted() {
    this.getData()
  },
  methods:{
    //获取数据
    async getData(page = 1){
      let res = await this.$API.sku.reqSkuList(this.page,this.limit)
      if (res.code===200){
        this.records = res.data.records
        this.total = res.data.total
      }
    },
    //改变每页的个数
    handleSizeChange(limit){
      this.limit = limit
      this.getData()
    },
    //改变当前页
    handleCurrentChange(page){
      this.page = page
      this.getData()
    },
    //下架
    async cancelSale(row){
      let res = await this.$API.sku.reqCancelSale(row.id)
      if (res.code===200){
        row.isSale = 0
        this.$message({type:'success',message:'下架成功'})
      }
    },
    //上架
    async onSale(row){
      let res = await this.$API.sku.reqOnSale(row.id)
      if (res.code===200){
        row.isSale = 1
        this.$message({type:'success',message:'上架成功'})
      }
    },
    //查看详情
    async detail(row){
      this.drawer = true
      let res = await this.$API.sku.reqDetail(row.id)
      if (res.code===200){
        this.skuInfo = res.data
      }
    },
    //删除sku
    async deleteSku(sku){
      try {
        let res = await this.$API.sku.reqDeleteSku(sku.id)
        if (res.code===200){
          this.$message({type:'success',message:'删除成功'})
          await this.getData()
        }else{
          this.$message({type:'info',message:'删除失败'})
        }
      }catch (e) {
        alert(e.message)
      }
    }
  }
}
</script>

<style scoped>
  .el-row .el-col-5{
    font-size: 18px;
    text-align: right;
    font-weight: bold;
  }
  .el-col{
    margin: 15px 10px;
  }
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 150px;
    margin: 0;
  }

  .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }

  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  }
</style>
